#include <iostream>
#include <Windows.h>
#include "Enemy_Maneger.h"
#include "Easy_Enemy.h"
#include "Normal_Enemy.h"
#include "Util.h"

int Enemy_Create_Time = GetTickCount();
int Enemy_Shoot_Time  = GetTickCount();

CEnemy_Maneger::CEnemy_Maneger(void) : m_pEnemyHead(NULL)
{

}

CEnemy_Maneger::~CEnemy_Maneger(void)
{
}

CEnemy* CEnemy_Maneger::Get_Last()
{
	if( !m_pEnemyHead )		return NULL;

	CEnemy *pTmp	=	m_pEnemyHead;
	for( ; pTmp->Get_Next() ; )
	{
		pTmp = pTmp->Get_Next();
	}

	return pTmp;
}

void CEnemy_Maneger::Create()
{
	int Ran_Num;
	

	CEnemy *pLast = Get_Last();

	if( Enemy_Create_Time + 600 <= GetTickCount())
	{
		Enemy_Create_Time = GetTickCount();
		Ran_Num = rand()%3+1;
		switch(Ran_Num)
		{
		case 2:
			if( !m_pEnemyHead )
			{
				m_pEnemyHead = new CNormal_Enemy();
			}
			else 
			{
				CEnemy *pTmp = new CNormal_Enemy();
				pLast->Set_Next(pTmp);
				pTmp->Set_Prev(pLast);
			}
			break;
		default:
			if( !m_pEnemyHead )
			{
				m_pEnemyHead = new CEasy_Enemy();
			}
			else 
			{
				CEnemy *pTmp = new CEasy_Enemy();
				pLast->Set_Next(pTmp);
				pTmp->Set_Prev(pLast);
			}
			break;
		}
			
	}
}
void CEnemy_Maneger::Delete_All()
{
	CEnemy* pTmp = m_pEnemyHead;

	for( ; pTmp ; )
	{
		pTmp->Shape_Delete();
		pTmp = this->Delete(pTmp);
		if( !pTmp )	break;
	}
	m_pEnemyHead = NULL;
}
CEnemy* CEnemy_Maneger::Delete(CEnemy* pTmp)
{
	if ( !m_pEnemyHead ) return NULL;

	CEnemy* pNext = pTmp->Get_Next();
	CEnemy* pPrev = pTmp->Get_Prev();

	if( !pPrev && !pNext )
		m_pEnemyHead	=	NULL;
	else if( !pPrev && pNext )
	{
		m_pEnemyHead	=	pTmp->Get_Next();	
		m_pEnemyHead->Set_Prev(NULL);
	}
	else if( pPrev && pNext )
	{
		pPrev->Set_Next(pNext);
		pNext->Set_Prev(pPrev);
	}
	else if( pPrev && !pNext )
		pPrev->Set_Next(NULL);

	delete pTmp;
	return pNext;
}

void CEnemy_Maneger::frameMove()
{
		if( !m_pEnemyHead )	return;

		CEnemy *pTmp = m_pEnemyHead;


		for( ; pTmp ; )
		{
			if( !pTmp->frameMove() )
			{
				pTmp->Effect();
				pTmp->Shape_Delete();
				pTmp = this->Delete(pTmp);
			}
			else pTmp = pTmp->Get_Next();
		}
}
void CEnemy_Maneger::render()
{
		CEnemy* pTmp = m_pEnemyHead;
		if( !m_pEnemyHead )	return;

		for( ; pTmp ; )
		{
			pTmp->render();
			pTmp = pTmp->Get_Next();
		}
}

void CEnemy_Maneger::Shoot_Enemy()
{
	if( !m_pEnemyHead ) return;
	if( Enemy_Shoot_Time + 1000 <= GetTickCount())
	{
		Enemy_Shoot_Time = GetTickCount();
		CEnemy *pTmp	=	m_pEnemyHead;
		E_Weapon_Maneger *pBullet = m_pEWeapon;
		int Ran_Num;
		int x, y;

		for( ; pTmp ; )
		{
			Ran_Num = rand()%3+1;

			switch(Ran_Num)
			{
			case 2:
				if( pTmp->Get_Enemy_Num() == 2 )
				{
					pTmp->Get_Pos( x, y );
					pBullet->Create( x, y );
					pBullet->Create( x-2, y-1);
					pBullet->Create( x+2, y-1);
				}
				break;
			default:
				break;
			}
			pTmp = pTmp->Get_Next();
		}
	}
	
}